﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.HitachiServiceRef;
using HitachiLib.Data;
using HitachiLib.Utils;

namespace HitachiApp
{
    public partial class MainPage : UserControl
    {
        Model.MainPageModel model;
        public MainPage()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            model = new Model.MainPageModel();
            this.DataContext = model;
        }

        private void client_GetResourceByUserPswdCompleted(object sender, GetResourceByUserPswdCompletedEventArgs e)
        {
            WCFClient.Release();
            try
            {
                ResourceData rd = e.Result;
            }
            catch (Exception err)
            {
                System.Windows.Browser.HtmlPage.Window.Alert(err.Message);
            }
            model.LoginAccount = Resource.GetUnique(e.Result);
            model.RefreshAccount();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            String username_str = username.Text;
            String password_str = password.Password;

            HitachiServiceClient client = WCFClient.GetClient();
            client.GetResourceByUserPswdCompleted +=
                new EventHandler<GetResourceByUserPswdCompletedEventArgs>(client_GetResourceByUserPswdCompleted);
            WCFClient.Retain();
            client.GetResourceByUserPswdAsync(username_str, password_str);
        }
    }
}
